<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <p><input type="text" @keyup.enter="add" v-model="text"></p>
        <div class="One">未完成({{num}})</div>
        <ul>
            <li v-for="(item,index) in list" :key="index" v-show="!item.flag" @dblclick="go(index)">
                <input type="checkbox" v-model="item.flag" @click="chooseOne(index)">
                <span v-if="item.edit" >{{item.title}}</span>
                <input type="text" v-if="!item.edit" ref="a" @blur="two(index)" v-model="test">
                <button @click="del(index)">删除</button>
                <span>{{item.time}}</span>
            </li>
        </ul>
        <div class="One">已完成({{sum}})</div>
        <ul>
            <li v-for="(item,index) in list" :key="index" v-show="item.flag"><input type="checkbox" v-model="item.flag"
                    @click="chooseOne(index)"><span>{{item.title}}</span> <button @click="del(index)">删除</button></li>
        </ul>
        <div class="foot">
            <div class="wu" v-show="sum==0">暂无记录</div>
             <button @click="clear" class="btn">批量删除</button>
        </div>
       
    </div>
</body>

</html>
<script>
    new Vue({
        el: '#app',
        data: {
            text: '',
            list: JSON.parse(localStorage.getItem('list')) || [],
            show: false,
            test: '',
            time:new Date().toLocaleString(),
            isShow:false
        },
        methods: {
            add() {
                if(this.text=='') return alert('不能为空')  
                let obj = { title: this.text, flag: false, edit: true ,time:this.time}
                this.list.push(obj)
                localStorage.setItem('list', JSON.stringify(this.list))
                this.text=''
            },
            del(index) {
                if (confirm('确定要删除吗？')) {
                    this.list.splice(index, 1)
                    localStorage.setItem('list', JSON.stringify(this.list))
                }
            },
            chooseOne(index) {
                this.list[index].flag = !this.list[index].flag
                localStorage.setItem('list', JSON.stringify(this.list))
            },
            go(index) {
                this.test=this.list[index].title 
                this.list[index].edit = !this.list[index].edit
                this.$nextTick(() => {
                    this.$refs.a[index].focus()
                })
                localStorage.setItem('list', JSON.stringify(this.list))
            },
            two(index) {
                this.list[index].edit = !this.list[index].edit
                this.list[index].title = this.test
                localStorage.setItem('list', JSON.stringify(this.list))
            },
            clear(){
                this.list = this.list.filter(item=>item.flag==false)
                localStorage.setItem('list',JSON.stringify(this.list))
            }
        },
        computed: {
            num() {
                return this.list.filter(item => item.flag == false).length
            },
            sum() {
                return this.list.filter(item => item.flag == true).length
            }
        }
    })
</script>
<style>
    * {
        margin: 0;
        padding: 0;
    }
    html,body,#app{
        width: 100%;
        height: 100%;
    }
    ul {
        width: 100%;
        list-style: none;
    }

    li {
        width: 80%;
        height: 40px;
        display: flex;
        justify-content: space-around;
        align-items: center;
        background-color: orange;
        margin: auto;
        margin-top: 10px;
    }

    .One {
        width: 100%;
        height: 50px;
        background-color: pink;
        line-height: 50px;
        margin-top: 20px;
        text-align: center;
        font-size: 24px;
    }
    .btn{
        width: 100px;
        height: 40px;
        background-color: brown;
        margin-top: 50px;
    }
    .foot{
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-top: 30px;
    }
</style>